package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.constant.Constants;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@SuppressWarnings("all")
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order>{
  //属性驱动封装省市区  sendAreaInfo  发件人省市区信息  recAreaInfo 收件人省市区信息  	
	@SuppressWarnings("all")
	private String sendAreaInfo;
	@SuppressWarnings("all")
	private String recAreaInfo;
	
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}
	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}
    
	
	@Action(value = "order_add", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String save_orde(){
		System.out.println("action保存");
		//封装省市区域信息
		Area sendarea  = new Area();
		//前台提交的省市区值格式为"北京市/北京市/昌平区"
		String[] sendAreaInfoArr  = sendAreaInfo.split("/");
		sendarea.setProvince(sendAreaInfoArr[0]);
		sendarea.setCity(sendAreaInfoArr[1]);
		sendarea.setDistrict(sendAreaInfoArr[2]);
		model.setSendArea(sendarea);
		
		
		//封装
		Area recare = new Area();
		String [] recAreaInfoArr = recAreaInfo.split("/");
		recare.setProvince(recAreaInfoArr[0]);
		recare.setCity(recAreaInfoArr[1]);
		recare.setDistrict(recAreaInfoArr[2]);
		model.setRecArea(recare);
		
		//关联当前登录的客户  封装客户的id   Customer_id 客户的编号
		
	   Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
	   model.setCustomer_id(customer.getId());
	   System.out.println("客户的id"+customer.getId());
	   //调用webService  将数据传递到bos_managerment
	   WebClient.create(Constants.BOS_MANAGEMENT_URL
				+ "/services/orderService/order")
		.type(MediaType.APPLICATION_JSON).post(model);
	   //调用webservic 将数据传递到bos_management
		return SUCCESS ;
		
	}
	
}
